<?php
//首页显示
namespace newMobile\Controller;
use Think\Controller;
class ShopController extends CommonController {
    public function index(){
      
    	$id=I('get.id');
    	//足迹
    	$isweixin=is_weixin();
    	if($isweixin){
    		$appid=C('weixin.APP_KEY');
			$appsecret=C('weixin.APP_SECRET');
			$weObj = new \Org\Util\Jssdk($appid,$appsecret);
			$wechat=$weObj->GetSignPackage();
			$this->assign('signPackage',$wechat);
    	}
      
    	if(session('user_id')){
    		$history['shop_id']=$id;
			$history['user_id']=session('user_id');
			$users_shophistory=M('users_shophistory');
			$rs_history=$users_shophistory->where($history)->select();
			if($rs_history){
				$users_shophistory->where($history)->delete();
			}else{
				$history['addtime']=time();
				$users_shophistory->add($history);
			}
    	}
    	$shop=M('shop');
    	$shop_info=$shop->find($id);
    	if(!$shop_info){
    		$this->error('该产品不存在！');
    	}
    	if($shop_info['ishidden']>0){
    		$this->error('该产品已下架！');
    	}
        if($shop_info['is_crowdfunding']){
            $crowdfunding=M('crowdfunding');
            $crowdfunding_info=$crowdfunding->where("goods_id=$id")->find();
            $crowdfunding_info['surplus_time']=ShengYu_Tian_Shi_Fen(strtotime($crowdfunding_info['end_time']));
            $crowdfunding_info['percent']=round( $crowdfunding_info['now_money']/$crowdfunding_info['total_money'] * 100 , 2);
            $this->assign('crowdfunding',$crowdfunding_info);
        }
       	if($_GET['rec_id']){
    		$rec_id=$_GET['rec_id'];
    		//注释后自己可以领自己的抵扣券
//    		if($rec_id==session('user_id')){
//				$this->redirect('shop/index', array('id' =>$id));
//    		}
//		 	$rs=M('order')
//		 		->join('yt_order_goods ON yt_order.order_id = yt_order_goods.order_id','left')
//		 		->where("yt_order_goods.goods_id=$id and yt_order.order_state='40' and yt_order.refund_state='0' and yt_order.buyer_id=$rec_id ")
//		 		->find();
			$shop_url=U('shop/index', array('id' =>$id));
            $rs=get_daiyan_status($id,$rec_id);
        
		 	if(!$rs){
		 		$this->error('该用户无代言该产品资格！',$shop_url);
		 	}else{
		 		if(session('user_id')){
		 			$daiyan_info['uid']=session('user_id');
			 		$daiyan_info['bid']=$_GET['rec_id'];
			 		$daiyan_info['shop_id']=$id;
                    $daiyan_info['time']=time();
                  
                 	$invoice=get_web_invoice();
                 
					$start_time=time()-60*60*24*$invoice['web_diyong'];
                  	$uid=session('user_id');
					$daiyan=M('daiyan')->where("bid=$rec_id and shop_id=$id and time>$start_time and uid=$uid ")->find();
			 		
			 		if(!$daiyan){
                        $daiyan_rs=M('daiyan')->add($daiyan_info);
                        $buser_id=session('user_id');
                        $buser_info=M('users')->find($buser_id);
                        if($buser_info['wx_h5openid']&&$shop_info['dikouquan']>0){
                            $keyword1='58代言'.$shop_info['dikouquan'].'元现金抵扣券';
                            $keyword2=date('Y-m-d H:i:s',time());
                            send_info_dikouquan($buser_info['wx_h5openid'],$keyword1,$keyword2);
                        }
                        if($daiyan_rs&&$shop_info['dikouquan']>0){
                            $this->assign('daiyan_rs',1);
                        }
			 		}
		 		}
		 	}
    	}
    	$daiyan_sale_where['uid']=session('user_id');
 		$daiyan_sale_where['shop_id']=$id;
    	$daiyan_sale=M('daiyan')->where($daiyan_sale_where)->find();
    	if($daiyan_sale){
    		$this->assign('daiyan_sale',$daiyan_sale);
    	}
    	$shop->where('id='.$id)->setInc('click_num',1);
    	$shop_info['shop_sku']=json_decode($shop_info['shop_sku'],true);
//  	dump($shop_info['shop_sku']);
//  	die;
    	$this->assign('shop_info',$shop_info);
    	//拼团
    	if($shop_info['tuanselect']==1){
    		$order=M('order');
    		$rs_tuan=$order
    			->field('yt_order.order_id,yt_order.buyer_name,yt_order.buyer_mobile,yt_users.user_pic,yt_order.order_id,yt_order.payment_time,yt_order.tuannum,yt_order.buyer_city')
    			->join("yt_order_goods on yt_order_goods.order_id=yt_order.order_id")
    			->join("yt_users on yt_users.id=yt_order.buyer_id")
    			->where("yt_order.tuanselect=1 and yt_order.tuan_is_first_id=0 and yt_order_goods.goods_id=$id and yt_order.order_state='20' and yt_order.refund_state=0" )
    			->group('yt_order.order_id')
    			->select();
    		foreach($rs_tuan as $key=>$v){
    			$num=get_tuan($v['order_id']);
    			$pt_nowtime=time();
				$close_time=$v['payment_time']+60*60*24;
				if($pt_nowtime<$close_time){
					if($num<$v['tuannum']){
						$v['num']=$v['tuannum']-$num;
						$new_rs_tuan[]=$v;
					}
				}
    		}
    		$this->assign('new_rs_tuan',$new_rs_tuan);
    		
    	}
    	//面包屑生成
    	$ticket_type=M('ticket_type');
    	$bids=explode(',', $shop_info['type_id']);
    	$bread[0]['href']='/';
    	$bread[0]['name']='首页';
    	foreach ($bids as $key => $value) {
    		if($key>0){
    			$type=$ticket_type->find($value);
    			$bread[$key]['href']=U('list/index',array('id' =>$value));
    			$bread[$key]['name']=$type['name'];
    		}
    	}
    	$this->assign('bread',$bread);
    	//获取相册
    	$shop_content=M('shop_content');
    	$shop_content_info=$shop_content->where('shop_id='.$id)->find();
    	$shop_content_info['config']=json_decode($shop_content_info['config'],true);
  
    	$this->assign('shop_content_info',$shop_content_info);
    	$image_array=explode(',',$shop_content_info['gallery']);
    	$arr_img;
    	foreach($image_array as $key => $value){
    		$arr_img[$key][0]=$value;
    		$arr_img[$key][1]=$value;
    		$arr_img[$key][2]=$value;
    	}
    	$arr_img=json_encode($arr_img);
    	$this->assign('arr_img',$arr_img);
    	//店铺详情
    	$store=M('store');
    	$store_template=M('store_template');
    	$store_info=$store->find($shop_info['store_id']);
    	$store_template_info=$store_template->where('store_id='.$shop_info['store_id'])->find();
    	$this->assign('store_template_info',$store_template_info);
    	$this->assign('store_info',$store_info);
    	//店铺所在地
    	$address['Province']=getProvinceInfo($store_info['province']);
    	$address['City']=getCityInfo($store_info['city']);
    	$address['District']=getDistrictInfo($store_info['district']);
    	$this->assign('address',$address);
    	//销量排行榜
    	$sale_num=$shop->where('store_id='.$shop_info['store_id'])->order('sale_num desc')->limit(8)->select();
    	//阅读排行榜
    	$click_num=$shop->where('store_id='.$shop_info['store_id'])->order('click_num desc')->limit(8)->select();
    	$this->assign('sale_num',$sale_num);
    	$this->assign('click_num',$click_num);
    	$order_goods_evaluate=M('order_goods_evaluate');
    	//图片评价
    	$img_evaluate_count=$order_goods_evaluate->where("goods_id=$id and  is_show=1 and  image <>''")->count();
    	//好评
    	$good_evaluate_count=$order_goods_evaluate->where("goods_id=$id and is_show=1 and (scores=5 or scores=4)")->count();
    	//差评
    	$bad_evaluate_count=$order_goods_evaluate->where("goods_id=$id and  is_show=1 and (scores=1 or scores=2)")->count();
    	//中评 
    	$zhong_evaluate_count=$order_goods_evaluate->where("goods_id=$id and is_show=1 and scores=3")->count();
    	$this->assign('img_evaluate_count',$img_evaluate_count);
    	$this->assign('good_evaluate_count',$good_evaluate_count);
    	$this->assign('bad_evaluate_count',$bad_evaluate_count);
    	$this->assign('zhong_evaluate_count',$zhong_evaluate_count);
    	//判断收藏
    	$users_storecollect=M('users_storecollect');
    	$users_shopcollect=M('users_shopcollect');
    	if(session('user_id')){
    		$newshopinfo['shop_id']=$id;
			$newshopinfo['user_id']=session('user_id');
			$rsshopinfo=$users_shopcollect->where($newshopinfo)->select();
			if($rsshopinfo){
				$rsshopinfo_status=1;
			}else{
				$rsshopinfo_status=0;
			}
			$newstoreinfo['shore_id']=$shop_info['store_id'];
			$newstoreinfo['user_id']=session('user_id');
			$rsstoreinfo=$users_storecollect->where($newstoreinfo)->select();
			if($rsstoreinfo){
				$rsstoreinfo_status=1;
			}else{
				$rsstoreinfo_status=0;
			}
		
   		}else{
	   		$rsshopinfo_status=0;
	   		$rsstoreinfo_status=0;
   		}
   		
    	$this->assign('rsshopinfo_status',$rsshopinfo_status);
    	$this->assign('rsstoreinfo_status',$rsstoreinfo_status);
    	//获取优惠券
    	$coupon_goods=M('coupon_goods');
    	$coupon_type_id=$coupon_goods->field('coupon_type_id')->where("goods_id=$id")->group('coupon_type_id')->select();
    	$coupon_type_ids;
    	foreach($coupon_type_id as $key=>$value){
    		if($key==0){
    			$coupon_type_ids=$value['coupon_type_id'];
    		}else{
    			$coupon_type_ids=','.$value['coupon_type_id'];
    		}
    	}
    	$coupon_type=M('coupon_type');
    	$map['coupon_type_id']  = array('in',$coupon_type_ids);
    	$map['range_type']  = 1;
    	$map['_logic'] = 'OR';
    	$coupons=$coupon_type->where($map)->select();
    	$coupons_i=0;
    	$new_coupons;
    	foreach($coupons as $key=>$value){
    		$start_time=strtotime($value['start_time']);
    		$end_time=strtotime($value['end_time']);
    		$nowtime=time();
    		if($start_time<=$nowtime && $end_time>=$nowtime){
    			$new_coupons[$coupons_i]=$value;
    			$coupons_i++;
    		}
    	}
   		$shop_count=M('shop')->where('ishidden=0 and store_id='.$shop_info['store_id'])->count();
   		$shop_store_count=M('users_storecollect')->where('shore_id='.$shop_info['store_id'])->count();
    	$this->assign('shop_count',$shop_count);
    	$this->assign('shop_store_count',$shop_store_count);
    	$this->assign('coupons',$new_coupons);
		$this->display();
	}
	
	function get_coupons(){
		$coupon_type=M('coupon_type');
		$coupon=M('coupon');
		$id=I('post.id');
		$uid=session('user_id');
		$coupon_info=$coupon_type->find($id);
		$count=$coupon->where("uid!=0 and coupon_type_id=$id")->count();
		$user_count=$coupon->where("uid=$uid and coupon_type_id=$id")->count();
		$coupon_info['lq']=0;
		if($user_count>=$coupon_info['max_fetch']&&$coupon_info['max_fetch']!=0){
			$coupon_info['lq']=1;
		}
		$baifen=($count/$coupon_info['count'])*100;
		$coupon_info['baifen']=$baifen;
		$this->ajaxReturn($coupon_info);
	}
	
	function get_coupons_save(){
		$id=I('post.id');
		if(session('user_id')){
			$uid=session('user_id');
			$coupon_type=M('coupon_type');
			$coupon=M('coupon');
			$coupon_info=$coupon_type->find($id);
			$count=$coupon->where("coupon_type_id=$id and uid=$uid")->count();
			if($count>=$coupon_info['max_fetch']&&$coupon_info['max_fetch']!=0){
				$data['code']=3;
				$data['message']='您已领取超过该优惠券最大领取个数 ！';
			}else{
				$rs=$coupon->where("coupon_type_id=$id and uid=0")->find();
				if($rs){
					$data['coupon_id']=$rs['coupon_id'];
					$data['uid']=$uid;
					$data['fetch_time']=date('Y-m-d H:i:s',time());
					$data['state']=1;
					$rs=$coupon->save($data);
					$data['code']=2;
					$data['message']='领取成功！';
				}else{
					$data['code']=1;
					$data['message']='没有剩余优惠券了！';
				}
			}
			
		}else{
			$data['code']=0;
			$data['message']='您还未登陆！！';
		}
		$this->ajaxReturn($data);
	}
	
	function comment(){
		
		//商品评价
		$last=I('get.last');
    	$amount=I('get.amount');
    	$datatype=I('get.datatype');
    	
    	if($datatype!=null){
    		
    		if($datatype==1){
    			$where_evaluate['image']=array('neq','');;
    		}
    		if($datatype==2){
    			$where_evaluate['_string'] = 'scores=5 or scores=4 ';
    		
    		}
    		if($datatype==3){
    			$where_evaluate['scores']='3';	
    		}
    		if($datatype==4){
    			$where_evaluate['_string'] = 'scores=1 or scores=2 ';
    		}
    	}
    	
    	$order_goods_evaluate=M('order_goods_evaluate');
    	$where_evaluate['goods_id']=I('get.id');
    	$where_evaluate['is_show']=1;
    	$evaluate_list=$order_goods_evaluate->where($where_evaluate)->order('id desc')->limit($last,$amount)->select();
    
    	foreach($evaluate_list as $key => $value){
    		$user_info=get_user_info($value['uid']);
    		if($value['is_anonymous']==1){
    			$user_info['username']='***【匿名】';
    		}
    		if($key==0){
    			$top='<div class="blank-div"></div>';
    		}else{
    			$top='';
    		}
           $user_info['username']=yc_phone($user_info['username']);
    		$info = $top.'<div class="goods-comment"><div class="user-info"><span class="face"><img src="'.$user_info['user_pic'].'" class="user_img"></span><span class="user-name">'.$user_info['username'].'</span></div><div class="goods-comment-text">'.$value['content'].'</div>';
			if($value['image']){
				$img_array=explode(',',$value['image']);
				$info.='<ul class="goods-comment-picture" id="gallery">';
				foreach($img_array as $k => $vo){
					$info.='<li><a data="'.$vo.'"><img src="'.$vo.'"></a></li>';
				}
				$info.='</ul>';
			};
			$info.='<div class="goods-comment-time">'.date('Y-m-d H:i:s',$value['addtime']).'</div>';
			if($value['explain_first']){
					$info.='<div class="goods-comment-again">
					<div class="text"><em class="type">【店家回复】：</em> '.$value['explain_first'].'
					</div></div>';
			}
			if($value['again_content']){
				$info.='<div class="goods-comment-again">
					<div class="text"><em class="type">【追加评价】：</em> '.$value['again_content'].'
					</div>';
					
				$img_array=explode(',',$value['again_image']);
				if($value['again_image']){
					$img_array=explode(',',$value['again_image']);
					$info.='<ul class="goods-comment-picture" id="gallery">';
					foreach($img_array as $k => $vo){
						$info.='<li><a data="'.$vo.'"><img src="'.$vo.'"></a></li>';
					}
					$info.='</ul>';
				};
				$info.='</div>';
			}
			if($value['again_explain']){
				$info.='<div class="goods-comment-again">
					<div class="text"><em class="type">【店家回复】：</em> '.$value['again_explain'].'
					</div></div>';
				
				
			}
			$info.='</div><div class="blank-div"></div>';
    	 	$res[]['info']=$info;
    	}
    	
    	$this->ajaxReturn($res);
	}
	
	function ctuan(){
		$id=I('get.id');
		$order_info=M('order')->find($id);
		$order_good=M('order_goods')->where('order_id='.$id)->find();
		if($order_info['tuan_is_first_id']==0){
			$tuan_is_first_id=$id;
		}else{
			$tuan_is_first_id=$order_info['tuan_is_first_id'];
		}
		$first=M('order')->find($tuan_is_first_id);
		if($first['refund_state']>0){
			$url=U('shop/index',array('id'=>$order_good['goods_id']));
			$this->error('该团已过期',$url);
		}
		$shop_info=M('shop')->where('id='.$order_good['goods_id'])->find();
		$shop_info['shop_sku']=json_decode($shop_info['shop_sku'],true);
		$num=10;
		$max=M('shop')->max('id');
		$min=M('shop')->min('id');
		$uid=session('user_id');
		$i=1;
		$flag=0;
		while($i<=$num){
			$rundnum=rand($min, $max);
			$flag_2=true;
			if($flag!=$rundnum){
				if($like_list){
					foreach($like_list as $key=>$value){
						if($rundnum==$value['id']){
							$flag_2=false;
						}
					}
				}
				if($flag_2){
					$rs=M('shop')->where('ishidden=0')->getById($rundnum);
					if(!empty($rs)){
						$like_list[]=$rs;
						$i++;
						$flag=$rundnum;
					}
				}
				
				
			}
			
		}
		
    	$this->assign('tuan_is_first_id',$tuan_is_first_id);
    	$this->assign('like_list',$like_list);
		$this->assign('order_info',$order_info);
		$this->assign('order_good',$order_good);
		$this->assign('shop_info',$shop_info);
		$this->assign('uid',$uid);
		$this->display();
	}
	
	function store(){
		$id=I('get.id');
		$shop=M('shop')->find($id);
		$shop_content=M('shop_content')->where('shop_id='.$id)->find();
		M('shop_content')->where('shop_id='.$id)->setInc('story_hits'); 
		$store_template=M('store_template')->where('store_id='.$shop['store_id'])->find();
		$store_info=M('store')->find($shop['store_id']);
		$this->assign('store_template',$store_template);
		$this->assign('store_info',$store_info);
		$this->assign('shop_content',$shop_content);
		$this->assign('shop',$shop);
		$this->display();
	}
	
}
?>
